const db = require('../db/index')
// const bcryptjs = require('bcryptjs')
// const jwt = require('jsonwebtoken')
// const config = require('../config')

exports.getList = (req, res) => {
    const { page, limit } = req.body
    db.getConnection((err, connection) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        let sqlStrArr = [
            'select * from article limit ?,1',
            'select * from user limit ?,1',
            'select * from song limit ?,1',
            'select * from video limit ?,1'
        ]
        let type = ["article", "user", "song", "video"]
        let ress = []
        for (let i = 0; i < limit; i++) {
            let _random = Math.floor(Math.random() * 4)
            connection.query(sqlStrArr[_random], page, (err, results) => {
                results[0]["contentType"] = type[_random]
                if (results[0]["userId"] && results[0]["userId"] >= 0) {
                    sqlStr = 'select * from user where id=?'
                    connection.query(sqlStr, results[0].userId, (err, result) => {
                        results[0]["user"] = result[0]
                        if (results[0]["coverUrlList"]) {
                            results[0]["coverUrlList"] = results[0]["coverUrlList"].split(",")
                        }
                        ress.push(results[0])
                    })
                } else ress.push(results[0])
            })
        }
        setTimeout(() => {res.send(ress) }, 100)
    })
}

